package com.example.telesys.controller;

import com.example.telesys.dto.BroadbandOrderDTO;
import com.example.telesys.dto.PhoneOrderDTO;
import com.example.telesys.dto.TrafficOrderDTO;
import com.example.telesys.pojo.*;
import com.example.telesys.service.CustomerService;
import com.example.telesys.service.CustomerorderService;
import com.example.telesys.service.SupplierService;
import com.example.telesys.utils.UuidUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    @Autowired
    private CustomerorderService customerorderService;

    @Autowired
    private SupplierService supplierService;


    /**
     * 由于订单与各个产品之间的属性名字不一致
     * 因此我们要使用相应的DTO来设置对应的属性
     * 要使用到事务
     **/
    @RequestMapping("/submitPhoneOrder")
    public ApiResponse submitPhoneOrder(
            @RequestBody(required = false) PhoneOrderDTO phoneOrderDTO) {
        Customerorder order = new Customerorder();
        order.setOrderid(UuidUtil.getUuid());
        order.setProductid(phoneOrderDTO.getPhoneid());
        order.setUsername(phoneOrderDTO.getUsername());
        order.setProductname(phoneOrderDTO.getPhonename());
        order.setProducttype(phoneOrderDTO.getPhonetype());
        order.setProductsize(phoneOrderDTO.getPhonesize());
        order.setProductcolor(phoneOrderDTO.getPhonecolor());
        order.setSupplierid(phoneOrderDTO.getSupplierid());
        order.setPrice(phoneOrderDTO.getPrice());
        order.setNumber(phoneOrderDTO.getNumber());
        order.setEmail(phoneOrderDTO.getEmail());
        order.setReceiver(phoneOrderDTO.getReceiver());
        order.setArea(phoneOrderDTO.getArea());
        order.setAddressdetail(phoneOrderDTO.getAddressdetail());
        order.setPhonenumber(phoneOrderDTO.getPhonenumber());

        //声明式事务管理
        try {
            customerorderService.insertPhoneOrder(order);
            supplierService.addPhonePriceTotal(phoneOrderDTO.getPrice() * phoneOrderDTO.getNumber(), phoneOrderDTO.getSupplierid());
        } catch (Exception e) {
            e.printStackTrace();
            return new ApiResponse(StatusCode.FAILED, StatusMessage.BUY_FAILED, phoneOrderDTO);
        }

        return new ApiResponse(StatusCode.SUCCESS, StatusMessage.BUY_SUCCESS, phoneOrderDTO);
    }

    /**
     * 由于订单与各个产品之间的属性名字不一致
     * 因此我们要使用相应的DTO来设置对应的属性
     **/
    @RequestMapping("/submitBroadbandOrder")
    public ApiResponse submitBroadbandOrder(
            @RequestBody(required = false) BroadbandOrderDTO broadbandOrderDTO) {
        Customerorder order = new Customerorder();
        order.setOrderid(UuidUtil.getUuid());
        order.setUsername(broadbandOrderDTO.getUsername());
        order.setProductid(broadbandOrderDTO.getBroadbandid());
        order.setProductname(broadbandOrderDTO.getBroadbandname());
        order.setProducttype(broadbandOrderDTO.getBroadbandtype());
        order.setPrice(broadbandOrderDTO.getPrice());
        order.setNumber(broadbandOrderDTO.getNumber());
        order.setEmail(broadbandOrderDTO.getEmail());
        order.setReceiver(broadbandOrderDTO.getReceiver());
        order.setArea(broadbandOrderDTO.getArea());
        order.setAddressdetail(broadbandOrderDTO.getAddressdetail());
        order.setPhonenumber(broadbandOrderDTO.getPhonenumber());

        try {
            customerorderService.insertBroadbandOrder(order);
            supplierService.addBroadPriceTotal(broadbandOrderDTO.getPrice() * broadbandOrderDTO.getNumber(), broadbandOrderDTO.getSupplierid());
        } catch (Exception e) {
            e.printStackTrace();
            return new ApiResponse(StatusCode.FAILED, StatusMessage.BUY_FAILED, broadbandOrderDTO);
        }
        return new ApiResponse(StatusCode.SUCCESS, StatusMessage.BUY_SUCCESS, broadbandOrderDTO);
    }


    /**
     * 由于订单与各个产品之间的属性名字不一致
     * 因此我们要使用相应的DTO来设置对应的属性
     **/
    @RequestMapping("/submitTrafficOrder")
    public ApiResponse submitTrafficOrder(
            @RequestBody(required = false) TrafficOrderDTO trafficOrderDTO) {
        Customerorder order = new Customerorder();
        order.setOrderid(UuidUtil.getUuid());
        order.setUsername(trafficOrderDTO.getUsername());
        order.setProductid(trafficOrderDTO.getTrafficid());
        order.setProductname(trafficOrderDTO.getTrafficname());
        order.setProducttype(trafficOrderDTO.getTraffictype());
        order.setPrice(trafficOrderDTO.getPrice());
        order.setNumber(trafficOrderDTO.getNumber());
        order.setEmail(trafficOrderDTO.getEmail());
        order.setReceiver(trafficOrderDTO.getReceiver());
        order.setArea(trafficOrderDTO.getArea());
        order.setAddressdetail(trafficOrderDTO.getAddressdetail());
        order.setPhonenumber(trafficOrderDTO.getPhonenumber());

        try {
            customerorderService.insertTrafficOrder(order);
            supplierService.addTrafficPriceTotal(trafficOrderDTO.getPrice() * trafficOrderDTO.getNumber(), trafficOrderDTO.getSupplierid());
        } catch (Exception e) {
            e.printStackTrace();
            return new ApiResponse(StatusCode.FAILED, StatusMessage.BUY_FAILED, trafficOrderDTO);
        }
        return new ApiResponse(StatusCode.SUCCESS, StatusMessage.BUY_SUCCESS, trafficOrderDTO);
    }

    /**
     * 客户查看自己的所有订单
     **/
    @RequestMapping("/selectMyOrders")
    public ApiResponse selectMyOrders(
            @RequestParam("username") String username,
            @RequestParam("currentPageNumber")Integer currentPageNumber,
            @RequestParam("currentPageSize")Integer currentPageSize) {
        PageInfo pageInfo = customerorderService.selectMyOrders(username, currentPageNumber, currentPageSize);
        return new ApiResponse(StatusCode.SUCCESS, StatusMessage.BUY_SUCCESS, pageInfo);
    }

    /**
     * 插入一条客户信息
     **/
    @RequestMapping("/addCustomer")
    public int addCustomer(@RequestParam("username") String username) {
        return customerService.addCustomer(username);
    }

    /**
     * 客户更改个人信息
     **/
    @RequestMapping("/updateCustomerInfo")
    public ApiResponse updateCustomerInfo(@RequestBody Customer customer) {
        int updateInfo = customerService.updateCustomerInfo(customer);
        if (updateInfo == 0) {
            return new ApiResponse(StatusCode.FAILED, StatusMessage.FAILED, updateInfo);
        }
        return new ApiResponse(StatusCode.SUCCESS, StatusMessage.SUCCESS, updateInfo);
    }

    /**
     * 查询客户个人信息
     **/
    @RequestMapping("/selectCustomerInfo")
    public ApiResponse selectCustomerInfo(@RequestParam("username") String username) {
        Customer customer = customerService.selectCustomerInfo(username);
        return new ApiResponse(StatusCode.SUCCESS, StatusMessage.SUCCESS, customer);
    }


    @RequestMapping("/selectAllCustomers")
    public PageInfo selectAllCustomers(
            @RequestParam("pageNumber") Integer pageNumber,
            @RequestParam("pageSize") Integer pageSize) {
        PageHelper.startPage(pageNumber, pageSize);
        List<Customer> list = customerService.selectAllCustomers();
        PageInfo pageInfo = new PageInfo(list, pageSize);
        return pageInfo;
    }

    @RequestMapping("/selectCustomersByName")
    public PageInfo selectCustomersByName(
            @RequestParam("customername") String customername,
            @RequestParam("pageNumber") Integer pageNumber,
            @RequestParam("pageSize") Integer pageSize) {
        PageHelper.startPage(pageNumber, pageSize);
        List<Customer> list = customerService.selectCustomersByName(customername);
        PageInfo pageInfo = new PageInfo(list, pageSize);
        return pageInfo;
    }

}

